var touringcar_collection = db.collection("touringcar");
(function() {
    // 构造函数
    var _con = function(info, user) {
        var self = this;
        self.num = 10;
        if (info) {
            /*printData("房车：", info, admin);
            self.type = "module";*/
            // 自有id
            self.info = info;
            // 创建者
            //self.creater = admin || {};
            self.id = "bycar_" + Math.random().toString(16).substr(2, 2) + (new Date()).getTime().toString(16);
            // 创建时间
            self.createtime = (new Date()).getTime();
            // 模块名称
            /*self.name = info.name;
            // 创建者
            self.creater = admin || {};
            self.creater_id = admin._id;
            // 图标
            //self.preview_img = info.preview_img;
            // 创建时间
            self.createtime = (new Date()).getTime();*/
        };
        if(user){
            self.user = user;
        }
    };

    // 导出类
    module.exports = _con;

    //查找房车
    _con.prototype.getlist = co.wrap(function*(){
        var self = this;
        var page = parseInt(self.info.page);
        page = Math.max(1,page)-1;
        var param = {
            skip: self.num * page,
            limit: self.num,
            sort: {
                createtime: -1
            }
        };
        var price = parseFloat(self.info.price);
        if(price>0){
            param.query.rentprice = {$gt:price};
        }
        var carmodul_collection = db.collection("carmodul");
        if(self.module){
            var obj_carmodul = yield mongo.findOne(carmodul_collection,{
                query:{
                    _id:self.module
                }
            });
            if(obj_carmodul){
                param.query.module = self.module;
            }
        }
        //return yield mongo.find(touringcar_collection,param);
        var car_data = yield mongo.find(touringcar_collection,param);
        var len = car_data.length;
        var arr_module = yield mongo.find(carmodul_collection);
        var module_name;
        for(var i=0;i<len;i++){
            module_name = null;
            for(var j=0;j<arr_module.length;j++){
                if(car_data[i].module==arr_module[j]._id){
                    module_name = arr_module[j].name;
                    break;
                }
            }
            car_data[i].module_name = module_name;
        }
        return car_data;
    });

    // 房车数量
    _con.prototype.count = co.wrap(function*() {
        var self = this;
        return yield mongo.count(touringcar_collection);
    });

    // 修改详情
    _con.prototype.getDetail = co.wrap(function*(_id) {
        var self = this;
        if(!_id){
            return printError('房车ID不能为空！');
        }
        var arr_car = yield mongo.findOne(touringcar_collection,{
            query:{
                _id:_id
            }
        });
        if(!arr_car){
            return printError('房车ID不存在！');
        }
        var modul_collection = db.collection('carmodul');
        var arr_module = yield mongo.findOne(modul_collection,{query:{_id:arr_car.module}});
        if(!arr_module){
            arr_car.module_name = arr_car.module;
        }else{
            arr_car.module_name = arr_module.name;
        }
        return arr_car;
    });

    //用户租赁房车
    _con.prototype.addTouringcar = co.wrap(function*(){
        var self = this;
        var data = self.info;
        if(!self.user){
            return printError('用户未登录！');
        }
        //var _u = yield JSON.parse(self.user.toString || "{}");
        var _u = self.user;
        data.buynumber = parseInt(data.buynumber);
        if(!data.car_id){
            return printError('房车不能为空！');
        }
        var car = yield mongo.findOne(touringcar_collection,{query:{_id:data.car_id}});
        if(!car){
            return printError('房车不存在！');
        }
        var user_collection = db.collection('user');
        var arr_user = yield mongo.findOneByObjectId(user_collection,_u._id);
        if(!arr_user){
            return printError('用户不存在！');
        }
        if(data.buynumber<=0){
            return printError('租借天数必须大于0！');
        }
        var price = car.rentprice*data.buynumber;
        var insert_data = {
            _id:self.id,
            user_id:_u._id,
            car_id:data.car_id,
            buynumber:data.buynumber,
            price:price,
            status:1,
            buytime : 0,
            createtime:self.createtime
        };
        var carorder_collection = db.collection("carorder");
        var _doc = yield mongo.insertOne(carorder_collection,insert_data);
        return insert_data;
    });
}());